<?php
 
ini_set('memory_limit','16M');

require_once( '/home/cyberpower678/Peachy/Init.php' );

$site = Peachy::newWiki( "cyberbot i" );

$site->set_runpage("User:Cyberbot I/Run/Speedy images");

$parentcategories = array ('Category:Wikipedia files with no copyright tag',
                           'Category:Wikipedia files with unknown source',
                           'Category:Wikipedia files with unknown copyright status',
                           'Category:Wikipedia files with no non-free use rationale',
                           'Category:Disputed non-free Wikipedia files',
                           'Category:Replaceable non-free use Wikipedia files',
                           'Category:Orphaned non-free use Wikipedia files',
                           );
 
$cats = array();
 
foreach ($parentcategories as $parentcat) {
    $cats = array_merge($cats,$site->categorymembers($parentcat,false,14));
}

$cats = array_filter($cats,'dates');
 
foreach ($cats as $cat) {
    $files = $site->categorymembers($cat,false,6);
 
    foreach ($files as $file) {
        $pages = initImage($file['title'])->get_usage();
        
                foreach ($pages as $page) {
                        open ($page,$file['title']);
                }

    }
}
 
 
 
 
function open ($title,$imagename) {
    $page = initPage($title);
 
    $text = $page->get_text();
 
    $regex = '/\[\[' . preg_replace (array('/^File/i','/[ _]/'),array('(?:(?:File)|(?:Image))','[ _]'),preg_quote($imagename,'/')) . '.*/is';
 
    preg_match_all($regex,$text,$matches);
 
    foreach ($matches[0] as $match) {
        $counter = 2;
        $position = 2;
 
        while ($position < strlen($match)) {
            if ($match[$position] == '[') {
                ++$counter;
            } elseif ($match[$position] == ']') {
                --$counter;
            }
 
            ++$position;
 
            if ($counter == 0) {
                break 1; //the while loop only
            }
        }
 
        $imageuse = substr($match,0,$position);
 
        if (preg_match('/[\|]?\{\{\s*Deletable[ \-]image[ \-]caption/i',$imageuse)) {
            continue;
        }
 
        $current = '';
        $template = 0;
        $link = false;
 
        for ($i = 2 ; $i < strlen($imageuse) ; $i++) {
            if ($current && !$template && !$link && ($imageuse[$i] == '|')) {
                $bits[] = $current;
                $current = '';
                continue;
            }
 
            if (!$template && !$link && ($imageuse[$i] == ']') && ($imageuse[$i + 1] == ']')) {
                if ($current) {
                    $bits[] = $current;
                } elseif ($imageuse[$i - 1] == '|') {
                    $bits[] = '';
                }
 
                break;
            }
 
            if (($imageuse[$i] == '{') && ($imageuse[$i + 1] == '{')) {
                $template++;
            }
 
            if (($imageuse[$i] == '}') && ($imageuse[$i + 1] == '}')) {
                $template--;
            }
 
            if (!$link && ($imageuse[$i] == '[') && ($imageuse[$i + 1] == '[')) {
                $link = true;
            }
 
            if ($link && ($imageuse[$i] == ']') && ($imageuse[$i + 1] == ']')) {
                $link = false;
            }
 
            $current .= $imageuse[$i];
        }
 
        $link = '[[' . $bits[0];
 
        array_shift($bits);
 
        $added = false;
        $addlink = '';
        $regex = '/^\s*(?:(?:thumb)|(?:frame)|(?:border)|(?:right)|(?:left)|(?:center)|(?:none)|(?:link=)|(?:\d+\s*px)|(?:\d+x\d+\s*px(?:px)?)\s*$)/i';
 
        for ($i = count($bits) - 1 ; $i >= 0 ; --$i) {
            $bit = $bits[$i];
 
            if (preg_match($regex,$bit)) {
                $addlink = '|' . $bit . $addlink;
            } else {
                if (!$added) {
                    $bit .= ' {{deletable image-caption}}';
                    $added = true;
                }
                $addlink = '|' . $bit . $addlink;
            }
        }
 
        if (!$added) {
            $addlink .= '|{{deletable image-caption}}';
        }
 
        $link .= $addlink . ']]';
 
        $text = str_replace($imageuse,$link,$text);
    }
 
    $page->edit($text,'[[Wikipedia:Bots/Requests for approval/Sambot 12|BOT]]: tagging [[:' . $imagename . ']] with {{[[Template:deletable image-caption|]]}}',false);
}
 
function dates (&$name) {
    $name = $name['title']; //strip out 'sortkey' field (hence pass by reference)
 
    if (preg_match('/\d{1,2} \w+ \d{4}$/',$name)) {
        return true;
    } else {
        return false;
    }
}

